package blazingbubble.network.lobby.strategy;

import blazingbubble.game.ILobbyGameEventListener;
import blazingbubble.game.command.JoinGameCommand;
import moltencore.network.protocol.CreateGameResponse;
import spark.network.protocol.Response;
import spark.network.strategy.IProtocolHandleStrategy;
import spark.network.protocol.Base;
import org.apache.log4j.Logger;
import spark.utility.OutWrapper;

import java.util.Collection;

public class CreateGameResponseStrategy implements IProtocolHandleStrategy<CreateGameResponse> {
	private Logger logger = Logger.getLogger(CreateGameResponseStrategy.class);

	private ILobbyGameEventListener lobbyGameEventListener;
	private JoinGameCommand joinGame;

	public void setJoinGameCommand(
			JoinGameCommand joinGameCommand) {
		this.joinGame = joinGameCommand;
	}

	public void setGameCreationListener(ILobbyGameEventListener lobbyGameEventListener) {
		this.lobbyGameEventListener = lobbyGameEventListener;
	}

	@Override
	public void handle(CreateGameResponse packet, OutWrapper<Response> acknowledgement, Collection<Base> additionalData) {
		logger.debug("CreateGameResponse: port " + packet.getPort() + "; message " + new String(packet.getMessage()));
		if(lobbyGameEventListener != null) {
			lobbyGameEventListener.gameSelected(packet.getPort());
			lobbyGameEventListener.hostPromoted(packet.getSession());
			joinGame.join();
		}
	}
}